package com.yenlien.traceyun.gateway.config;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author: bigwangy@sina.com
 * @create: 2020-10-27
 * @description:
 **/

//@Component
public class RequestAuthFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest serverHttpRequest = exchange.getRequest();
        String method = serverHttpRequest.getMethodValue();
        serverHttpRequest.getHeaders().getFirst("token");
//        if(!"POST".equals(method)){
//            ServerHttpResponse response = exchange.getResponse();
//            String message= "请求限制";
//            byte[] bits = message.getBytes(StandardCharsets.UTF_8);
//            DataBuffer buffer = response.bufferFactory().wrap(bits);
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            //指定编码，否则在浏览器中会中文乱码
//            response.getHeaders().add("Content-Type", "text/plain;charset=UTF-8");
//            return response.writeWith(Mono.just(buffer));
////        }

        return chain.filter(exchange);
    }

    /**
     * @return int 数字越大优先级越低
     */
    @Override
    public int getOrder() {
        return 0;
    }
}
